package cn.shop.dys.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.adapter.CollectAdapter;
import cn.shop.dys.bean.ProductInfo;
import cn.shop.dys.constant.CommonConstant;

/**
 * 收藏
 * Created by liulu on 2016/4/18.
 */
public class CollectActivity extends AppCompatActivity implements View.OnClickListener {
    private ListView lvScj;
    private LinearLayout scj_ll;
    private List<ProductInfo> products;
    private TextView scj_title;
    private TextView scjProduct;
    private ImageView back;
    private Button clear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        lvScj = (ListView) findViewById(R.id.lv_scj);
        scjProduct = (TextView) findViewById(R.id.scj_product);
        scj_ll = (LinearLayout) findViewById(R.id.scj_ll);
        scj_title = (TextView) findViewById(R.id.tv_common_index_titile);
        back = (ImageView) findViewById(R.id.iv_common_back);
        clear = (Button) findViewById(R.id.scj_clear);

        initData();
    }

    private void initData() {
        requestData();
        scj_title.setText("收藏夹");
        lvScj.setEmptyView(scj_ll);
        back.setOnClickListener(this);
        clear.setOnClickListener(this);
        scjProduct.setOnClickListener(this);
        lvScj.setOnItemClickListener(clickItem);
    }
    AdapterView.OnItemClickListener clickItem = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(CollectActivity.this,ProductActivity.class);
            intent.putExtra("pId",products.get(position));
            startActivity(intent);
            finish();
        }
    };


    private void requestData() {
        HttpUtils httpUtils = new HttpUtils();
        RequestParams requestParams = new RequestParams();
        requestParams.addHeader("userid", "20428");
        requestParams.addBodyParameter("page", "0");
        requestParams.addBodyParameter("pageNum", "10");
        httpUtils.send(HttpRequest.HttpMethod.POST, CommonConstant.SCJ_URL, requestParams, new RequestCallBack<String>() {



            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                String result = responseInfo.result;
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String jsonProduct = jsonObject.getString("productList");
                    Gson gson = new Gson();
                    products = gson.fromJson(jsonProduct, new TypeToken<List<ProductInfo>>() {
                    }.getType());
                    CollectAdapter cxkbAdapter = new CollectAdapter(CollectActivity.this, products);
                    lvScj.setAdapter(cxkbAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_common_back:
                finish();
                break;
            case R.id.scj_clear:
                lvScj.setVisibility(View.GONE);
                break;
            case R.id.scj_product:
                startActivity(new Intent(CollectActivity.this, MainActivity2.class));
                break;
            default:break;
        }

    }
}
